<template>
  <div v-loading="listLoading" class="tree-list">
    <vab-query-form>
      <vab-query-form-left-panel :span="24" style="margin-bottom: 20px">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item label="策略名称:">
            <el-input
              v-model.trim="queryForm.strategyName"
              placeholder="策略名称"
              clearable
              @keyup.enter.native="queryData()"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      ref="singleTable"
      :data="filterList"
      border
      highlight-current-row
      style="width: 100%"
      @current-change="handleRowCurrentChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column v-if="checkbox" type="selection"></el-table-column>
      <el-table-column
        prop="strategyName"
        label="请选择价格策略"
        style="background-color: #ffcc66"
      ></el-table-column>
    </el-table>
  </div>
</template>

<script>
  import { getAllList } from '@/api/priceStrategyManagement'

  export default {
    name: 'PriceStrategyTreeList',
    props: {
      checkbox: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        total: 0,
        listLoading: true,
        list: [],
        filterList:[],
        queryForm:{
          strategyName : ''
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      async fetchData() {
        this.listLoading = true
        this.listLoading = true
        this.list = await getAllList()
        this.filterList = this.list
        setTimeout(() => {
          this.listLoading = false
        }, 300)

        const { id } = this.$route.params
        //默认选择Tree第一个
        if (this.list.length > 0) {
          let selectItem = this.list[0]
          if (id) {
            selectItem = this.list.find((o) => o.id == id)
          }

          this.$emit('strategy-info', selectItem)
          this.setCurrent(selectItem)
        }

        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },

      setCurrent(row) {
        this.$refs.singleTable.setCurrentRow(row)

        this.$nextTick(() => {
          this.$refs.singleTable.toggleRowSelection(row)
        })
      },
      handleRowCurrentChange(row) {
        if (row.id) {
          this.$emit('strategy-info', row)
        }
      },
      handleSelectionChange(val) {
        this.$emit('selection-change', val)
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },

      queryData(){
        let vm = this
        this.filterList = this.list.filter(p=>p.strategyName.toLowerCase().includes(this.queryForm.strategyName))
         //默认选择Tree第一个
        if (this.filterList.length > 0) {
          let selectItem = this.filterList[0]
          this.$emit('strategy-info', selectItem)
          this.setCurrent(selectItem)
        }
      }
    },
  }
</script>
<style lang="scss" scoped>
  .tree-list {
    box-sizing: border-box;
  }
</style>
